package com.sskj.qiangjin.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.bean.RankBean;
import com.sskj.qiangjin.http.HttpConfig;
import com.sskj.qiangjin.presenter.RankActivityPresenter;
import com.sskj.qiangjin.ui.ArrowAdapter;
import com.sskj.qiangjin.util.HideUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_RANK)//排行榜
public class RankActivity extends BaseActivity<RankActivityPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.tv_rank_type)
    TextView tv_rank_type;
    @BindView(R.id.spinner_type)
    Spinner spinner_type;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private ModelRx2DataSource<RankBean> dataSource;
    private MVCCoolHelper<List<RankBean>> mvcHelper;
    private int name = 0;
    private int type = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_rank;
    }

    @Override
    public RankActivityPresenter getPresenter() {
        return new RankActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_rank));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_rank, new SlimInjector<RankBean>() {
            @Override
            public void onInject(RankBean data, IViewInjector injector, List payloads) {
                String palyerName = data.getRealname();
                if (HideUtil.isInteger(palyerName)) {
                    if (palyerName.length() == 11) {
                        palyerName = HideUtil.getPhoneHide(palyerName);
                    }
                }
                if (TextUtils.isEmpty(data.getUpic())) {
                    injector.image(R.id.iv_rank_header, App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_wolf));
                } else {
                    injector.imageCircle(R.id.iv_rank_header, BaseHttpConfig.BASE_URL + data.getUpic());
                }
                injector.visibility(R.id.iv_item_rank, slimAdapter.getData().indexOf(data) < 3 ? View.VISIBLE : View.GONE)
                        .visibility(R.id.tv_item_rank, slimAdapter.getData().indexOf(data) > 2 ? View.VISIBLE : View.GONE)
                        .text(R.id.tv_item_rank, slimAdapter.getData().indexOf(data) + 1 + "")
                        .text(R.id.tv_rank_name, palyerName)
                        .text(R.id.tv_bet_total_money, name == 0 ? data.getZuigao() : data.getLeiji())
                        .background(R.id.iv_item_rank, String.valueOf(slimAdapter.getData().indexOf(data)).equals("0") ?
                                App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_cup_1)
                                : String.valueOf(slimAdapter.getData().indexOf(data)).equals("1") ?
                                App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_cup_2)
                                : App.INSTANCE.getResources().getDrawable(R.mipmap.app_icon_cup_3));

            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(false);
        initSpinner();
        RxAdapterView.itemSelections(spinner)
                .subscribe(pos -> {
                    name = pos;
                    tv_rank_type.setText(name == 0 ? "最高盈利" : "投注总额");
                    updataList();
                });
        RxAdapterView.itemSelections(spinner_type)
                .subscribe(pos -> {
                    type = pos;
                    updataList();
                });
    }

    private void initSpinner() {
        List<String> payNames = Arrays.asList(getResources().getStringArray(R.array.app_rank_names));
        List<String> payTypes = Arrays.asList(getResources().getStringArray(R.array.app_rank_types));
        ArrowAdapter spinnerAdapter = new ArrowAdapter(this,
                R.layout.app_item_rank_type, payNames);
        spinnerAdapter.setDropDownViewResource(R.layout.mine_item_pay_type_drop);
        spinner.setAdapter(spinnerAdapter);
        spinner.setSelection(0);
        spinner.setDropDownVerticalOffset(10);

        ArrowAdapter spinnerTypeAdapter = new ArrowAdapter(this,
                R.layout.app_item_rank_type, payTypes);
        spinnerTypeAdapter.setDropDownViewResource(R.layout.mine_item_pay_type_drop);
        spinner_type.setAdapter(spinnerTypeAdapter);
        spinner_type.setSelection(0);
        spinner_type.setDropDownVerticalOffset(10);
    }

    public void updataList() {
        dataSource = new ModelRx2DataSource<RankBean>(
                (ModelRx2DataSource.OnLoadSource<RankBean>) page -> mPresenter.getRankList(
                        type == 0 ? "usdt" :
                                type == 1 ? "cny" : "pb",
                        name == 0 ? "zuigao" : "leiji"
                        , page, 10), 10) {
            @Override
            public boolean hasMore() {
                return false;
            }
        };
        mvcHelper.setDataSource(dataSource);
        mvcHelper.refresh();
    }
}
